package org.zjt.spark.book

import org.apache.spark.{SparkConf, SparkContext}

/**
  * DESC   得到平均数
  *
  * @author
  * @create 2017-06-30 上午10:42
  **/
object AvgNum extends App {
  var sparkConf = new SparkConf().setMaster("local[2]").setAppName("AvgNum")
  val sc = new SparkContext(sparkConf)


  val avg = sc.textFile("/Users/zhangjuntao/IdeaProjects/myproject/hw-bigdata/scala-demo/src/main/resource/mllib/Avg.log").map {
    line => {
      val array = line.split("\t")
      (array.head, array.last.toDouble)
    }
  }.groupByKey().mapValues {
    value => {
      value.sum / value.size
    }
  }


  println(avg.collect().mkString("\n"))
  sc.stop()
}
